package com.cirkana.viewer.map.location;

import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.cirkana.viewer.map.MapCommunicator;

public class MapLocationManager {

	private Context context;
	private LocationListener locationListener;

	public MapLocationManager(Context context, MapCommunicator communicator) {
		this.context = context;
		this.locationListener = new MapLocationListener(communicator);
	}

	protected LocationManager getLocationManager() {
		return (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
	}

	public String[] getLocationProviders() {
		return getLocationProviders(true);
	}

	public String[] getLocationProviders(boolean enabledOnly) {
		List<String> providersList = this.getLocationManager().getProviders(enabledOnly);
		String[] providersArray = new String[providersList.size()];
		for (int iii = 0; iii < providersList.size(); iii++)
			providersArray[iii] = providersList.get(iii);
		return providersArray;
	}

	public void start(String locationProvider) {
		this.getLocationManager().requestLocationUpdates(locationProvider, 0, 0, this.locationListener);
	}

	public Location getLastKnownLocation(String locationProvider) {
		return this.getLocationManager().getLastKnownLocation(locationProvider);
	}

	public void changeLocation(Location location) {
		this.locationListener.onLocationChanged(location);
	}

	public static class MapLocationListener implements LocationListener {

		private MapCommunicator comm;

		public MapLocationListener(MapCommunicator comm) {
			this.comm = comm;
		}

		@Override
		public void onLocationChanged(Location location) {
			this.comm.addChangeLocationMessage(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

	}
}
